
###################### Figure 3a-3b: Predicted effects of past concession 
## and future stake on territorial dispute initiation by weaker states
#based on Model 1
sim_b1 <- sim_cluster(ModelResults = b1, n.sim = 1000, data = data, 
                      clusterid = "ddyad")

###Figure 3(a)
##set simulation
ModelResults = b1
val1 =0; val2 = 20; intervals = 1
varname2_val = seq(val1, val2, by = intervals)
varname2 = "tgt_landneighbors2"
df <- array(NA, c(length(varname2_val), 4))


for (i in 1:length(varname2_val)){
       X1 <- model.matrix(ModelResults)
       X1[, varname2] = varname2_val[i]
       fd = apply(apply(X1, 1, function (x) plogis(sim_b1 %*% x)), 1, mean)
       
       df[i, 1] <- varname2_val[i]
       df[i, 2] <- mean(fd)
       df[i, 3:4] <- quantile(fd, probs = c(.05,.95))
}

colnames(df) <- c("X", "mean", "lo", "hi")
df <- as.data.frame(df)

p_b1 = ggplot(df, aes(x=X, y=mean)) +
       theme_gray() +
       geom_ribbon(aes(ymin = lo, ymax = hi), alpha=.2, color = NA) +
       geom_line(aes(y = mean, x = X), size = 0.5, alpha = 0.75) +
       scale_x_continuous(name = "Num. of land neighbors",
                          breaks = seq(val1, val2, by = 1)) +
       theme(axis.text = element_text(size=12),
             plot.title = element_text(hjust = 0.5,size = 30, face = "bold"),
             axis.title=element_text(size=12)) + 
       ylab("Pr(initiation)") 
ggsave("./figures/p_b1.pdf", plot = p_b1,  width = 8, height = 6)


###  Figure 3(b)
## for backdown_terr1
val1 =0; val2 = 1; intervals = 0.1
varname2_val = seq(val1, val2, by = intervals)
varname2 = "backdown_terr1"
df <- array(NA, c(length(varname2_val), 4))

for (i in 1:length(varname2_val)){
       X1 <- model.matrix(ModelResults)
       X1[, varname2] = varname2_val[i]
       fd = apply(apply(X1, 1, function (x) plogis(sim_b1 %*% x)), 1, mean)
       
       df[i, 1] <- varname2_val[i]
       df[i, 2] <- mean(fd)
       df[i, 3:4] <- quantile(fd, probs = c(.05,.95))
}

colnames(df) <- c("X", "mean", "lo", "hi")
df <- as.data.frame(df)

p2_b1 = ggplot(df, aes(x=X, y=mean)) +
       theme_gray() +
       geom_ribbon(aes(ymin = lo, ymax = hi), alpha=.2, color = NA) +
       geom_line(aes(y = mean, x = X), size = 0.5, alpha = 0.75) +
       scale_x_continuous(name = "Past yielding",
                          breaks = seq(val1, val2, by = 0.1)) +
       theme(axis.text = element_text(size=12),
             plot.title = element_text(hjust = 0.5,size = 30, face = "bold"),
             axis.title=element_text(size=12)) + 
       ylab("Pr(initiation)") 
ggsave("./figures/p2_b1.pdf", plot = p2_b1,  width = 8, height = 6)



###################### Figure 4: Marginal effects of explanatory variables on
####################### territorial dispute initiation by weaker states 
#based on Model 1
fd_b1 <- density_ridgeplot(ModelResults = b1, n.sim = 1000, 
                  varname = c("tgt_landneighbors2","backdown_terr1",
                              "backdown_nonterr1", "joint_demo",
                              "defensetgt", "offensechal",
                              "land_neighbor","vicratio"),
                  data = data, 
                  clusterid = "ddyad", 
                  val1 = 0, 
                  val2 = 1) + theme_gray() +
       scale_y_discrete(labels=c("Past yielding (nonterritory)", "Past yielding",
                                  "Defensive alliance (target)", "Joint democracy",
                                  "Land neighbor", "Offensive alliance (challenger)",
                                  "Num. of land neighbors",
                                  "Exp. victory prob. ratio")) +
       labs(x = "First difference", y = "") + theme(axis.text = element_text(size=14),
                                    plot.title = element_text(hjust = 0.5),
                                    axis.title=element_text(size=14)) 
ggsave("./figures/fd_b1.pdf", plot = fd_b1,  width = 8, height = 10)


###################### Figure 5(a)-5(b): Comparing the effects of past concession 
####################### and future stake across models
## Figure 5(a)
landneigbor <- coef_comparison(ModelResults = list(b1, b2, b3, b4, b5),
                               n.sim = 1000, varname = "tgt_landneighbors2",
                     data = data, val1 = 0, val2 = 1, clusterid = "ddyad") +
       theme_gray() + labs(x = "", y = "") + ggtitle("") + 
       theme(axis.text = element_text(size=14),
             plot.title = element_text(hjust = 0.5),
             axis.title=element_text(size=14))
ggsave("./figures/landneigbor.pdf", plot = landneigbor,  width = 7, height = 6)

## Figure 5(b)
backdown <- coef_comparison(ModelResults = list(b1, b2, b3, b4, b5),
                               n.sim = 1000, varname = "backdown_terr1",
                               data = data, val1 = 0, val2 = 1, clusterid = "ddyad") +
       theme_gray() + labs(x = "", y = "") + ggtitle("") + 
       theme(axis.text = element_text(size=14),
             plot.title = element_text(hjust = 0.5),
             axis.title=element_text(size=14))

ggsave("./figures/backdown.pdf", plot = backdown,  width = 7, height = 6)



